package vy.org.app.page.printlabel.group;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.vy.dal.DALCtx;
import org.vy.dal.dao.VyPrintLabelGroupDAO;
import org.vy.dal.dao.VyPrintLabelGroupDO;
import org.vy.dal.seq.GroupPrintLabelSeqGenerator;

import vy.org.app.common.BaseAction;
import vy.org.app.common.ModuleEnum;

public class SaveGroupPrintLabelAction extends BaseAction {

	@Override
	public ActionForward executeAction(ActionMapping mapping,
			ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response) {
		String ctx = ""; 
		String msg = ""; 
		try {
			String[] subIds = request.getParameterValues("subId");
			String[] ids = request.getParameterValues("ID");
			String labelAddress = request.getParameter("labelAddress");
			String ops = request.getParameter("ops");
			String groupId = null;
			int idx = 0;

			DALCtx.ctx().beginTransaction();

			if ("addToGroup".equals(ops)) {
				for (int i = 0; i < subIds.length; i++) {
					if (subIds[i].equals(labelAddress)) {
						idx = i;
						break;
					}
				}

				VyPrintLabelGroupDO dto = VyPrintLabelGroupDAO
						.findBySubscriptionId(subIds[idx]);
				groupId = dto.getPrintLabelGroupId();
				if (groupId == null || "".equals(groupId)) {
					groupId = GroupPrintLabelSeqGenerator.getInstance()
							.getNextSequence();
				}
				for (int i = 0; i < subIds.length; i++) {
					VyPrintLabelGroupDO newdto = new VyPrintLabelGroupDO();
					newdto.setSubscriptionId(subIds[i]);
					VyPrintLabelGroupDAO.deleteBySubscriptionId(newdto);
				}
				for (int i = 0; i < ids.length; i++) {
					VyPrintLabelGroupDO newdto = new VyPrintLabelGroupDO();
					if (i == idx) {
						newdto.setIsLabelAddress(true);
					}else
						newdto.setIsLabelAddress(false);
					newdto.setVyId(ids[i]);
					newdto.setSubscriptionId(subIds[i]);
					newdto.setPrintLabelGroupId(groupId);
					VyPrintLabelGroupDAO.insert(newdto);
				}
				ctx = "Success";
				msg = "Successfully Added to group";
			}
			if ("createNewGroup".equals(ops)) {
				for (int i = 0; i < subIds.length; i++) {
					if (subIds[i].equals(labelAddress)) {
						idx = i;
						break;
					}
				}
				groupId = GroupPrintLabelSeqGenerator.getInstance()
						.getNextSequence();
				boolean added = false;
				for (int i = 0; i < subIds.length; i++) {
					VyPrintLabelGroupDO newdto = new VyPrintLabelGroupDO();
					newdto.setSubscriptionId(subIds[i]);
					VyPrintLabelGroupDAO.deleteBySubscriptionId(newdto);
				}
				for (int i = 0; i < ids.length; i++) {
					VyPrintLabelGroupDO newdto = new VyPrintLabelGroupDO();
					if (!added && i == idx) {
						newdto.setIsLabelAddress(true);
						added = true;
					}else{
						newdto.setIsLabelAddress(false);
					}
					newdto.setVyId(ids[i]);
					newdto.setSubscriptionId(subIds[i]);
					newdto.setPrintLabelGroupId(groupId);
					VyPrintLabelGroupDAO.insert(newdto);
				}
				ctx = "Success";
				msg = "Successfully created new group";
			}

			if ("removeFromGroup".equals(ops)) {
				for (int i = 0; i < subIds.length; i++) {
					if (subIds[i].equals(labelAddress)) {
						idx = i;
						break;
					}
				}

				VyPrintLabelGroupDO dto = VyPrintLabelGroupDAO.findBySubscriptionId(subIds[0]);
				groupId = dto.getPrintLabelGroupId();

				for (int i = 0; i < subIds.length; i++) {
					VyPrintLabelGroupDO newdto = new VyPrintLabelGroupDO();
					newdto.setSubscriptionId(subIds[i]);
					VyPrintLabelGroupDAO.deleteBySubscriptionId(newdto);
				}
				
				List<VyPrintLabelGroupDO> lst = VyPrintLabelGroupDAO.findByGroupId(groupId);
				
				for (VyPrintLabelGroupDO odt : lst) {
					if(odt.getIsLabelAddress() != null){
						odt.setIsLabelAddress(false);
						VyPrintLabelGroupDAO.update(odt);
					}
					if (odt.getSubscriptionId().equals(labelAddress)) {
						odt.setIsLabelAddress(true);
						VyPrintLabelGroupDAO.update(odt);
					}
				}
				ctx = "Success";
				msg = "Successfully removed from group";
			}
			request.setAttribute("CTX",ctx);
			request.setAttribute("MSG",msg);
			DALCtx.ctx().commitTransaction();
			
			return mapping.findForward("success");
		} catch (Exception e) {
			ctx = "Error";
			msg = "Failed to perform request, kindly try later";
			e.printStackTrace();
		}

		request.setAttribute("CTX",ctx);
		request.setAttribute("MSG",msg);
		DALCtx.ctx().rollbackTransaction();
		return mapping.findForward("error");
	}

	@Override
	protected ModuleEnum getModuleId() {
		return ModuleEnum.EDIT_SUBSCRIPTION;
	}

}
